<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
$tabs=array(
	array('op'=>'get','title'=>'更新城市区域'),
	array('op'=>'upcountry','title'=>'更新国家'),
	array('op'=>'uptext','title'=>'生成text/json'),
);
if($_RQ['op']=='get'){
	$return['item']=array();
	$return['htmls']=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'post' => 'post',
		  'edit'=>array(
				array ('type' =>'tip','title' => '<red>[温馨提示]：不用经常更新，官方有更新要求时，或者1年更新一次！'),
			),
	);
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	//插入地图数据
	pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_area;');
	require ROOT_D.'/update/update_area.php';
	pdo_query('DROP TABLE IF EXISTS '.TABLE_PRE.'core_city;');
	require ROOT_D.'/update/update_city.php';
	exi('更新成功！');
}
if($_RQ['op']=='upcountry'){
	$return['item']=array();
	$return['htmls']=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'post' => 'postcountry',
		  'edit'=>array(
				array ('type' =>'tip','title' => '<red>[温馨提示]：不用经常更新，官方有更新要求时，或者10年更新一次！'),
			),
	);
	exi($return);
}
if($_RQ['op']=='postcountry'){
	exi('更新成功！');
}
if($_RQ['op']=='uptext'){
	$return['item']=array();
	$return['htmls']=array(
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'post' => 'uptextpost',
		  'edit'=>array(
				array ('type' => 'tip','title' => '<red>[温馨提示]：生成text/json;前端读取使用'),
				array ('name' => 'type','type' => 'radio','title' => '生成类型','options'=>'types'),
			 ),
	);
	$return['types']=array(1=>'text',2=>'json');
	exi($return);
}
if($_RQ['op']=='uptextpost'){
	$post=get_postdata();
	$type=$post['type'];
	$area_data=pdo_getall('core_area',array('parent_code'=>'000000000000'),array('code','title'),'title','code asc');
	foreach($area_data as &$city){
		$city=pdo_getall('core_area',array('parent_code'=>$city['code']),array('code','title'),'title');
		foreach($city as &$district){
			$district=pdo_getall('core_area',array('parent_code'=>$district['code']),array('code','title'),'title');
			foreach($district as &$street){
				$street=pdo_getall('core_area',array('parent_code'=>$street['code']),array('code','title'),'title');
				foreach($street as &$v){
					$v=$v['title'];
				}
			}
		}
	}
	if($type==1){
		$text=serialize($area_data);
		file_put_contents(ROOT_D.'/core/resource/area_town.txt',$text);
	}elseif($type==2){
		$text=ijson_encode($area_data);
		file_put_contents(ROOT_D.'/core/resource/area_town_json.txt',$text);
	}
	exi('生成成功！');
}
?>